<?php
namespace app\admin\controller;
use app\common\controller\Admin;
use app\common\model\FeedBack as FeedBackModel;
/**
 * Description of Feedback
 * @author 高磊<1455209597@qq.com>
 */
class Feedback extends Admin{
    /**
     * 反馈首页
     */
    public function index() {
        $param = [];
        $order = 'f.create_time desc';
        $list = FeedBackModel::getFeedBackList($param, $order);
        $this->assign('list', $list);
        $this->setMeta('问题反馈');
        return view('feedback/index');
    }
    /**
     * 编辑反馈
     */
    public function edit($id) {
        $place = model('FeedBack');
        if ($this->request->isPost()) {
            $data=$this->request->param();
            if(strlen($data['content'])>300){
                return $this->error('内容不能超过100个字');
            }
            unset($data['username']);
            $result = model('feedback')->save($data, ['id' => $id]);
            if ($result !== false) {
                return $this->success("编辑成功", url('index'));
            } else {
                return $this->error($place->getError());
            }
        } else {
            $info =\think\Db::name('feedback f')
                    ->join('user u','u.id = f.user_id')
                    ->where(array('f.id' => $id))
                    ->field('f.id,u.username,u.nickname,f.create_time,f.content')
                    ->find();
            $info['create_time']=date('Y-m-d H:i:s',$info['create_time']);
            if (!$info) {
                return $this->error("非法操作！");
            }
            $data = array(
                'info' => $info,
                'keyList' => $place->keyList,
            );
            $this->assign($data);
            $this->setMeta("编辑内容");
            return $this->fetch('public/edit');
        }
    }
    /**
     *  删除反馈
     */
    public function delete() {
        $param = $this->request->param();
        $id = $param['id'];
        if (empty($id)) {
            return $this->error("非法操作！");
        }

        $map['id'] = array('IN', $id);
        $result = \think\Db::name('feedback')->where($map)->delete();

        if (false !== $result) {
            //记录行为
            action_log('delete_feedback', 'feedback', $result, session('auth_user.uid'));
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        }
    }
}
